const actions = {
    // 登录操作
    login({ commit }, { token, user_info }) {
        return new Promise((resolve) => {
            commit('LOGIN', { token, userInfo: user_info });
            resolve();
        });
    },
    
    // 登出操作
    logout({ commit }) {
        return new Promise((resolve) => {
            commit('LOGOUT');
            resolve();
        });
    },
    
    // 更新用户信息
    updateUserInfo({ commit }, userInfo) {
        return new Promise((resolve) => {
            commit('SET_USER_INFO', userInfo);
            resolve();
        });
    },
    
    // 更新token
    updateToken({ commit }, token) {
        return new Promise((resolve) => {
            commit('SET_TOKEN', token);
            resolve();
        });
    },

    // 设置登录后返回路径（不持久化）
    setRedirectAfterLogin({ commit }, path) {
        return new Promise((resolve) => {
            commit('SET_REDIRECT_AFTER_LOGIN', path)
            resolve()
        })
    },
    clearRedirectAfterLogin({ commit }) {
        return new Promise((resolve) => {
            commit('CLEAR_REDIRECT_AFTER_LOGIN')
            resolve()
        })
    },

    // 更新手机号
    updatePhone({ commit }, phone) {
        return new Promise((resolve) => {
            commit('SET_PHONE', phone);
            resolve();
        });
    },
    // 设置选中师傅
    setSelectedMaster({ commit }, master) {
        return new Promise((resolve) => {
            commit('SET_SELECTED_MASTER', master)
            resolve()
        })
    },
    // 设置预约信息
    setReservation({ commit }, reservation) {
        return new Promise((resolve) => {
            commit('SET_RESERVATION', reservation)
            resolve()
        })
    },
    // 设置选中规格
    setSelectedSpec({ commit }, spec) {
        return new Promise((resolve) => {
            commit('SET_SELECTED_SPEC', spec)
            resolve()
        })
    },
    // 清空订单选择
    clearOrderSelection({ commit }) {
        return new Promise((resolve) => {
            commit('CLEAR_ORDER_SELECTION')
            resolve()
        })
    },
    
    // 地址相关actions
    // 获取地址列表（如果store中没有数据则从接口获取）
    async getAddressList({ commit, state, getters }) {
        // 如果store中已有地址数据，直接返回
        if (state.addressList && state.addressList.length > 0) {
            return state.addressList;
        }
        
        // 否则从接口获取
        try {
            const { getAddressList } = await import('@/api/index.js');
            const res = await getAddressList();
            if (res.errno === 0) {
                const addressList = res.data || [];
                
                // 确保所有地址的ID都是数字类型
                const normalizedAddressList = addressList.map(address => ({
                    ...address,
                    id: Number(address.id),
                    isDefault: Number(address.isDefault)
                }));
                
                commit('SET_ADDRESS_LIST', normalizedAddressList);
                
                // 设置默认地址ID
                const defaultAddress = normalizedAddressList.find(item => item.isDefault === 1);
                if (defaultAddress) {
                    commit('SET_DEFAULT_ADDRESS', defaultAddress.id);
                }
                
                return normalizedAddressList;
            }
            return [];
        } catch (error) {
            console.error('获取地址列表失败:', error);
            return [];
        }
    },
    
    // 添加地址
    addAddress({ commit }, address) {
        return new Promise((resolve) => {
            commit('ADD_ADDRESS', address);
            resolve();
        });
    },
    
    // 更新地址
    updateAddress({ commit }, { id, address }) {
        return new Promise((resolve) => {
            commit('UPDATE_ADDRESS', { id, address });
            resolve();
        });
    },
    
    // 删除地址
    deleteAddress({ commit }, id) {
        return new Promise((resolve) => {
            commit('DELETE_ADDRESS', id);
            resolve();
        });
    },
    
    // 设置默认地址
    setDefaultAddress({ commit }, id) {
        return new Promise((resolve) => {
            commit('SET_DEFAULT_ADDRESS', id);
            resolve();
        });
    },
    
    // 设置选中地址
    setSelectedAddress({ commit }, address) {
        return new Promise((resolve) => {
            commit('SET_SELECTED_ADDRESS', address);
            resolve();
        });
    },
    getAddressUrl({commit}, url) {
        return new Promise((resolve) => {
            commit('GET_ADDRESS_URL', url);
            resolve();
        });
    },
    // 设置闲置下单商品信息
    setIdleOrderItem({ commit }, payload) {
        commit('SET_IDLE_ORDER_ITEM', payload)
    },
    // 清空闲置下单商品信息
    clearIdleOrderItem({ commit }) {
        commit('CLEAR_IDLE_ORDER_ITEM')
    },
    
    // 小区选择相关 actions
    // 设置当前城市 { province, city }
    setSelectedCity({ commit }, payload) {
        return new Promise((resolve) => {
            commit('SET_SELECTED_CITY', payload)
            resolve()
        })
    },
    
    // 设置小区列表
    setCommunityList({ commit }, list) {
        return new Promise((resolve) => {
            commit('SET_COMMUNITY_LIST', list)
            resolve()
        })
    },
    
    // 设置选中的取货小区
    setSelectedPickupCommunity({ commit }, community) {
        return new Promise((resolve) => {
            commit('SET_SELECTED_PICKUP_COMMUNITY', community)
            resolve()
        })
    },
    
    // 设置选中的送货小区
    setSelectedDeliveryCommunity({ commit }, community) {
        return new Promise((resolve) => {
            commit('SET_SELECTED_DELIVERY_COMMUNITY', community)
            resolve()
        })
    },
    
    // 设置小区选择类型
    setCommunitySelectType({ commit }, type) {
        return new Promise((resolve) => {
            commit('SET_COMMUNITY_SELECT_TYPE', type)
            resolve()
        })
    },
    
    // 清空小区选择数据
    clearCommunitySelection({ commit }) {
        return new Promise((resolve) => {
            commit('CLEAR_COMMUNITY_SELECTION')
            resolve()
        })
    }
}

export default actions;